import { useUserStore } from '@/stores'

/**
 * 字符权限校验
 * @param {string[]} value 校验值
 * @returns {boolean}
 */
export function checkPermi(value: string[]): boolean {
  if (value && Array.isArray(value) && value.length > 0) {
    const userStore = useUserStore()
    const permissions = userStore.permissions
    const all_permission = "*:*:*"

    const hasPermission = permissions.some((permission: string) => {
      return all_permission === permission || value.includes(permission)
    })

    return hasPermission
  }
  return false
}

/**
 * 角色权限校验
 * @param {string[]} value 校验值
 * @returns {boolean}
 */
export function checkRole(value: string[]): boolean {
  if (value && Array.isArray(value) && value.length > 0) {
    const userStore = useUserStore()
    const roles = userStore.roles
    const super_admin = "admin"

    const hasRole = roles.some((role: string) => {
      return super_admin === role || value.includes(role)
    })

    return hasRole
  }
  return false
}